﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;

public class Yja_model :MonoBehaviour, IPointerDownHandler, IPointerEnterHandler, IPointerExitHandler
{
    public Yj_model yj;

    public Texture2D cursorTexture;
    private CursorMode cursorMode = CursorMode.Auto;
    private Vector2 hotSpot = Vector2.zero;

    // Use this for initialization
    void Start () {
        yja_init();
    }
	
    // Update is called once per frame
	void Update () {
		
	}

    //初始化,
    void yja_init() {
        //调用Yj_model 中定义的元件参数
        GameObject go = transform.parent.gameObject;
        yj = (Yj_model)go.GetComponent(typeof(Yj_model));
        //Debug.Log(yj.yjb.name);
        cursorTexture = (Texture2D)Resources.Load("hand", typeof(Texture2D));

    }

    //鼠标进入变手形
    public void OnPointerEnter(PointerEventData eventData)
    {
        //transform.localScale *= 4f;
        Cursor.SetCursor(cursorTexture, hotSpot, cursorMode);
    }
    //鼠标移出变指针
    public void OnPointerExit(PointerEventData eventData)
    {

       // transform.localScale /= 4f;
        Cursor.SetCursor(null, Vector2.zero, cursorMode);

    }

    //实现接口调用该方法,点击原件显示原件图
    public void OnPointerDown(PointerEventData eventData)
    {
         Debug.Log("yja:"+this.name);
        if (Input.GetMouseButtonDown(0))
        {
            show_yjb();
        }
        
    }
    //显示元件b
    void show_yjb() {
       
        if (yj.yjb)
        {
            yj.yjb.SetActive(true);
            
            StartCoroutine(MoveToPosition());
        }
        //Debug.Log("maincam:" + maincam.transform.position.x+","+ maincam.transform.position.y + ","+ maincam.transform.position.z);
        //Debug.Log("yjb:" + yj.yjb0.transform.position.x + "," +yj.yjb0.transform.position.y + "," + yj.yjb0.transform.position.z);
    }

  



    IEnumerator MoveToPosition()
    {
        GameObject maincam = GameObject.Find("Main Camera");
        Vector3 target = yj.yjb0.transform.position - Vector3.forward * 1f;
        Quaternion targetRotation=Quaternion.Euler(0, 0, 0);
        if (yj.name == "lamp1")
        {
            target = yj.yjb0.transform.position - Vector3.up * 1f;
            targetRotation = Quaternion.Euler(-90f, 0, 0);
        }
        if (yj.name == "sw1")
        {
            target = yj.yjb0.transform.position - Vector3.right * 1f;
            targetRotation = Quaternion.Euler(0, 90f, 0);
           // maincam.GetComponent<Camera>().fieldOfView = 20;
        }
        while (maincam.transform.position != target)
        {
            maincam.transform.position = Vector3.MoveTowards(maincam.transform.position, target, 10 * Time.deltaTime);               
            yield return 0;
        }
        while (maincam.transform.rotation != targetRotation)
        {
            if (yj.name == "lamp1")
            {
                maincam.transform.rotation = Quaternion.Slerp(maincam.transform.rotation, targetRotation, Time.deltaTime * 10);//设置摄像机rotation 正对着lamp1
            }
            if (yj.name == "sw1")
            {
                maincam.transform.rotation = Quaternion.Slerp(maincam.transform.rotation, targetRotation, Time.deltaTime * 10);
            }
            yield return 1;

        }
        yj.yja.SetActive(false);
        Cursor.SetCursor(null, Vector2.zero, cursorMode);
    }



}
